package com.source.coupon.controller;

import com.source.coupon.annotation.IgnoreResponseAdvice;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * 描述: ribbon应用controller
 *
 * @author li
 * @date 2019/12/10
 */
@RestController
@Slf4j
public class RibbonController {

    /**
     * rest客户端
     */
    private final RestTemplate restTemplate;

    @Autowired
    public RibbonController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 通过 ribbon 调用模板微服务
     * @return TemplateInfo
     */
    @GetMapping("/info")
    @IgnoreResponseAdvice
    public TemplateInfo getTemplateInfo(){
        String url = "http://eureka-client-coupon-template/coupon-template/info";
        return restTemplate.getForEntity(url,TemplateInfo.class).getBody();
    }

    /**
     * 模板微服务元信息
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class TemplateInfo{
        private Integer code;
        private String message;
        private List<Map<String,Object>> data;
    }
}
